# This file is part of the Net::Tube module.
#
# Net::Tube is Free Software; you can use, redistribute it and/or
# modify it under the terms of either:
#
# a) the GNU General Public License as published by the Free Software
#    Foundation, either version 2, or (at your opinion) any later
#    version, or
#
# b) the "Artistic License" which comes with Perl.
#
use strict;
use warnings;
use Test::Simple tests => 5;

use Net::Tube;
use Net::Tube::Loader::File;

sub test_new {
    my $x = Net::Tube::Loader::File->new();
    unless ( defined $x && ref $x ) {
        print "#object not created \n";
        return 0;
    }
    unless ( $x->isa("Net::Tube::Loader::File") ) {
        print "# wrong object type created\n";
        return 0;
    }
    return 1;
}

ok( test_new(), "test new" );

sub test_load_simple {
    my $m = Net::Tube::Machine->new();
    my $f = Net::Tube::Loader::File->new();

    $f->set_config_entry( 'loadSource', 'tdata/simple_1.xml' );

    $m->add_filter($f);
    my $tv = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<A>foo</A>\n";
    my $data;
    eval { $data = $m->execute(); };
    if ($@) {
        print "# internal error: $@\n";
        return 0;
    }
    unless ( defined $data && length $data ) {
        print "# tube machine generated no output\n";
        return 0;
    }
    unless ( $tv eq $data ) {
        print
"# tube returned an unexpected result\n received '$data'\nexpected '$tv'\n";
        return 0;
    }
    return 1;
}

ok( test_load_simple(), "load simple" );

sub test_load_double {
    my $m = Net::Tube::Machine->new();
    my $f = Net::Tube::Loader::File->new();

    $f->add_config_entry( 'loadSource', 'tdata/simple_1.xml' );
    $f->add_config_entry( 'loadSource', 'tdata/simple_2.xml' );

    $m->add_filter($f);
    my $tv =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<A>foo</A>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<B>foo</B>\n";
    my $data;
    eval { $data = $m->execute(); };
    if ($@) {
        print "# internal error: $@\n";
        return 0;
    }
    unless ( defined $data && length $data ) {
        print "# tube machine generated no output\n";
        return 0;
    }
    unless ( $tv eq $data ) {
        print
"# tube returned an unexpected result\n received '$data'\nexpected '$tv'\n";
        return 0;
    }
    return 1;
}

ok( test_load_double(), "load double" );

sub test_load_from_bucket_info1 {
    my $m = Net::Tube::Machine->new();
    my $c =
      Net::Tube::Loader::File->new(
        { loadSource => 'tdata/local_config1.xml' } );
    my $f = Net::Tube::Loader::File->new( { loadXPath => '/config/file' } );

    my $tv = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<A>foo</A>\n";
    my $data;
    eval { $data = $m->execute( [ $c, $f ] ); };
    if ($@) {
        print "# internal error: $@\n";
        return 0;
    }
    unless ( defined $data && length $data ) {
        print "# tube machine generated no output\n";
        return 0;
    }
    unless ( $tv eq $data ) {
        print
"# tube returned an unexpected result\n received '$data'\nexpected '$tv'\n";
        return 0;
    }
    return 1;
}

ok( test_load_from_bucket_info1(), "load from bucket infos 1" );

sub test_load_from_bucket_info2 {
    my $m = Net::Tube::Machine->new();
    my $c =
      Net::Tube::Loader::File->new(
        { loadSource => 'tdata/local_config.xml' } );
    my $f = Net::Tube::Loader::File->new( { loadXPath => '/config/file' } );
    my $tv = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<A>foo</A>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<B>foo</B>\n";
    my $data;    
    eval { $data = $m->execute( [ $c, $f ] ); };
    if ($@) {
        print "# internal error: $@\n";
        return 0;
    }
    unless ( defined $data && length $data ) {
        print "# tube machine generated no output\n";
        return 0;
    }
    unless ( $tv eq $data ) {
        print
"# tube returned an unexpected result\n received '$data'\nexpected '$tv'\n";
        return 0;
    }
    return 1;
}

ok( test_load_from_bucket_info2(), "load from bucket infos 2" );

